﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DotNet.WebUI.Admin
{
    public partial class DictList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Category"] = GetQueryString("Category", "0");

                FillData();
            }
        }

        private void FillData()
        {
            int pageIndex = pagData.CurrentPageIndex;
            int pageSize = pagData.PageSize;

            string _Sql = @"SELECT Count(*) 
                FROM Dict 
                WHERE Category = {0} ";
            _Sql = string.Format(_Sql, ViewState["Category"].ToString());

            pagData.RecordCount = (int)ExecuteScalar(_Sql);

            _Sql = @"SELECT * 
                FROM Dict
                WHERE Category = {0}
                ORDER BY DictID ASC ";
            _Sql = string.Format(_Sql, ViewState["Category"].ToString());

            rptData.DataSource = ExecuteDataTable(_Sql, pageIndex, pageSize);
            rptData.DataBind();
        }

        protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName.ToLower())
            {
                case "edit":
                    Server.Transfer(string.Format("DictEdit.aspx?Operate=Edit&Category={0}&DictID={1}", ViewState["Category"].ToString(), e.CommandArgument));
                    break;
                case "del":
                    string sql = @"DELETE FROM Dict
                        WHERE DictID = '{0}' ";
                    sql = string.Format(sql, e.CommandArgument.ToString());
                    ExecuteNonQuery(sql);
                    break;
            }
            FillData();
        }

        protected void pagData_PageChanged(object sender, EventArgs e)
        {
            FillData();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("DictEdit.aspx?Operate=New&Category={0}", ViewState["Category"].ToString()));
        }
    }
}